﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Mvc;

namespace CoolCode.Web.Mvc.UI.Grid
{
    public class GridPanelRenderer<T> : HtmlTableGridRenderer<T>, IPagerRenderer, IToolbarRenderer where T : class
    {
        protected IPagerModel PagerModel { get; private set; }
        //protected IPageable PagerSource { get; private set; }
        protected IToolbarModel ToolbarModel { get; private set; }
        protected string Title { get; private set; }

        public GridPanelRenderer(IPagerModel pagerModel, IToolbarModel toolbarModel, string title)
        {
            PagerModel = pagerModel;
            //PagerSource = pagerSource;
            ToolbarModel = toolbarModel;
            Title = title;
        }

        protected override void RenderGridStart()
        {
            base.RenderGridStart();
            RenderTitle();
            RenderToolbar();
        }

        protected override void RenderGridEnd(bool isEmpty)
        {
            RenderPager();
            base.RenderGridEnd(isEmpty);
        }

        protected virtual void RenderPager()
        {
            if (PagerModel != null && PagerModel.DataSource != null && PagerModel.DataSource.TotalRecords > 0)
                Render(PagerModel, PagerModel.DataSource, Writer, Context);
        }

        protected virtual void RenderToolbar()
        {
            if (ToolbarModel != null && ToolbarModel.Buttons != null && ToolbarModel.Buttons.Count > 0)
                Render(ToolbarModel, Writer, Context);
        }

        protected virtual void RenderTitle()
        {
            if (!string.IsNullOrEmpty(Title))
            {
                Writer.Write("<div class=\"mDiv\">");
                Writer.Write("<div class=\"ftitle\">");
                Writer.Write(Title);
                Writer.Write("</div>");
                Writer.Write("</div>");
            }
        }

        public void Render(IPagerModel pagerModel, IPageable dataSource, TextWriter output, ViewContext viewContext)
        {
            output.Write("<div class=\"pDiv\">");
            //output.Write("<div class=\"pDiv2\">");
            Pager pager = new Pager(dataSource, viewContext.HttpContext, viewContext.RequestContext, viewContext.RouteData.Values);
            //if (!string.IsNullOrEmpty(pagerModel.UpdateTarget))
            //{
            string updateTarget = JQueryHelper.ForamtElement(pagerModel.UpdateTarget ?? pagerModel.Container);
            pager = pager.OnClick(string.Format("ajax.post(this.href,'{0}');", updateTarget))
                .Css("pDiv2");
            //}
            output.Write(pager);
            //output.Write("</div>");
            output.Write("</div>");
        }

        public void Render(IToolbarModel Model, TextWriter output, ViewContext viewContext)
        {
            output.Write("<div class=\"tDiv\">");
            output.Write("<div class=\"tDiv2\">");

            foreach (var button in Model.Buttons)
            {
                var b = ((ToolbarButton)button);
                output.Write("<div class=\"fbutton\">");
                output.Write("<div>");
                output.Write(string.Format("<span class=\"{0}\" onclick=\"{2}\">{1}</span></div>", b.Css, b.Name, b.ClickHandler()));
                output.Write("</div>");
            }

            output.Write("</div>");
            output.Write("<div style=\"clear: both;\">");
            output.Write("</div>");
            output.Write("</div>");
        }
    }
}
